package com.wordlearning.app.service.impl;

import com.wordlearning.app.entity.VoiceSetting;
import com.wordlearning.app.repository.VoiceSettingRepository;
import com.wordlearning.app.service.VoiceSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class VoiceSettingServiceImpl implements VoiceSettingService {

    private final VoiceSettingRepository voiceSettingRepository;

    @Autowired
    public VoiceSettingServiceImpl(VoiceSettingRepository voiceSettingRepository) {
        this.voiceSettingRepository = voiceSettingRepository;
    }

    @Override
    @Transactional
    public VoiceSetting createVoiceSetting(VoiceSetting voiceSetting) {
        // 检查用户是否已经有语音设置
        Optional<VoiceSetting> existingSetting = voiceSettingRepository.findByUserId(voiceSetting.getUserId());
        if (existingSetting.isPresent()) {
            throw new IllegalArgumentException("Voice setting already exists for user id: " + voiceSetting.getUserId());
        }
        voiceSettingRepository.insert(voiceSetting);
        return voiceSetting;
    }

    @Override
    public Optional<VoiceSetting> getVoiceSettingById(Long id) {
        return voiceSettingRepository.selectById(id);
    }

    @Override
    public Optional<VoiceSetting> getVoiceSettingByUserId(Long userId) {
        return voiceSettingRepository.findByUserId(userId);
    }

    @Override
    @Transactional
    public VoiceSetting updateVoiceSetting(Long id, VoiceSetting voiceSetting) {
        VoiceSetting existingSetting = voiceSettingRepository.selectById(id)
                .orElseThrow(() -> new IllegalArgumentException("Voice setting not found with id: " + id));

        existingSetting.setVoiceType(voiceSetting.getVoiceType());
        existingSetting.setSpeed(voiceSetting.getSpeed());
        existingSetting.setPitch(voiceSetting.getPitch());
        existingSetting.setVolume(voiceSetting.getVolume());
        existingSetting.setLanguageCode(voiceSetting.getLanguageCode());

        voiceSettingRepository.updateById(existingSetting);
        return existingSetting;
    }

    @Override
    @Transactional
    public void deleteVoiceSetting(Long id) {
        voiceSettingRepository.selectById(id)
                .orElseThrow(() -> new IllegalArgumentException("Voice setting not found with id: " + id));
        voiceSettingRepository.deleteById(id);
    }

    @Override
    @Transactional
    public VoiceSetting getOrCreateDefaultVoiceSetting(Long userId) {
        return voiceSettingRepository.findByUserId(userId)
                .orElseGet(() -> {
                    VoiceSetting defaultSetting = new VoiceSetting();
                    defaultSetting.setUserId(userId);
                    defaultSetting.setVoiceType("expr-voice-5-m"); // 使用TTS服务中实际存在的默认男性语音
                    defaultSetting.setSpeed(1.0);
                    defaultSetting.setPitch(1.0);
                    defaultSetting.setVolume(1.0);
                    defaultSetting.setLanguageCode("en-US");
                    voiceSettingRepository.insert(defaultSetting);
                    return defaultSetting;
                });
    }
}